package uk.ac.bath;

import java.util.logging.Level;
import java.util.logging.Logger;
import uk.ac.bath.factory.EnvironmentFactory;

import java.awt.BorderLayout;
import java.awt.Container;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import uk.ac.bath.environment.EnvironmentIF;
import uk.ac.bath.legacy.EnvironmentFactoryIF;
import uk.ac.bath.gui.ControlPanel;
import uk.ac.bath.gui.GuiUpdater;
import uk.ac.bath.environment.View;
import uk.ac.bath.util.TweakReader;

public class LifeApplet extends JApplet //implements Observer
{

    View panel;
    
    EnvironmentIF env;


//     public void setEnvironment(Environment env) {
//     }
    public void init() {



        Object[] options = EnvironmentFactory.getOptions().toArray();

        Object ans = options[1];

        
        if (true) {
            ans = JOptionPane.showInputDialog(this,
                    "Choose one", "Input",
                    JOptionPane.INFORMATION_MESSAGE, null,
                    options, options[0]);
        }

        String paramBase = "/params/";



        //	paramBase="file:///home/pjl/public_html/uk/ac/bath/gui/";

        EnvironmentFactoryIF provider=((EnvironmentFactoryIF) ans);
        try {
            env = provider.create();
     
        String param = paramBase + provider.getParameterFileName();

        System.out.println(param);
        try {

            InputStream reader = EnvironmentFactory.class.getResourceAsStream(param);
            BufferedReader bin = new BufferedReader(new InputStreamReader(reader));
               TweakReader.readTweakValues(bin,env.getTweaks());
         //   env.readTweakValues(bin);
        } catch (FileNotFoundException e) {
            System.err.println(" parameter file not found " + e + param.toString());
        } catch (IOException e) {
            System.err.println(" ERROR " + e.getMessage());
        }

        //EnvironmentFactory.select(ans, paramBase);

        panel = env.createSimulationView();

        Container p;
        
    //   if (isApplet) {
            JFrame f = new JFrame();
            p = f.getContentPane();
            p.setLayout(new BorderLayout());
            p.add(panel, BorderLayout.CENTER);
            p.add(new ControlPanel(panel, env), BorderLayout.SOUTH);
            f.validate();
            f.setVisible(true);
            f.setExtendedState(JFrame.MAXIMIZED_BOTH);
            add(new JLabel(" Use the popup window "));
//        } else {
//            setLayout(new BorderLayout());
//            add(panel, BorderLayout.CENTER);
//            add(new ControlPanel(panel, env), BorderLayout.SOUTH);
//            validate();
//        }
               } catch (Exception ex) {
            Logger.getLogger(LifeApplet.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public void start() {
        env.setGuiUpdater(new GuiUpdater(panel));
    }

    /** 
     * Called by the browser or applet viewer to inform this Applet that
     * it should stop its execution. It is called when the Web page that
     * contains this Applet has been replaced by another page, and also
     * just before the Applet is to be destroyed. If you do not have any
     * resources that you need to release (such as threads that you may
     * want to stop) you can remove this method.
     */
    public void stop() {
        // provide any code that needs to be run when page
        // is replaced by another page or before Applet is destroyed 
    }

    /**
     * Called by the browser or applet viewer to inform this Applet that it
     * is being reclaimed and that it should destroy any resources that it
     * has allocated. The stop method will always be called before destroy. 
     * If you do not have any resources that you need to release you can 
     * remove this method.
     */
    public void destroy() {
        // provide code to be run when Applet is about to be destroyed.
    }

    public String getAppletInfo() {
        return "Title: GM Demo Applet\n" +
                "Author: P.J.Leonard\n";
    }

    public String[][] getParameterInfo() {
        // provide parameter information about the applet
        String paramInfo[][] = {
            {"firstParameter", "1-10", "description of first parameter"},
            {"secondParameter", "boolean", "description of second parameter"}
        };
        return paramInfo;
    }
}
